date=$(date +"%d %b %Y")
fullname=$(cat /etc/passwd | grep -i ^$LOGNAME: | cut -d: -f5 | cut -d, -f1)
if test $# -eq 0 || test "$1" = "-d"; then
  clear
  echo "$fullname's Shopping List as of today, $date"
  echo "-----------------------------------------------------"
  echo ""
  itemcount=$(grep -c -i "|-" ushop.$LOGNAME) 
  purcount=$(grep -c -i -v "|-" ushop.$LOGNAME) 
  if test $(grep -c '|' ushop.$LOGNAME) -eq 0; then
    echo "No items on the list."
  else
    echo "Remaining items"
    itemsum=0
    pursum=0
    grep '|-' ushop.$LOGNAME > item.$LOGNAME
    awk -F "|" ' BEGIN {total=0 }
    { printf "  %6d. %-30s %14.2f\n", NR, $1, $2
      total+=$2 } 
    END { printf "                                          --------------\n" 
          printf "Total remaining items %33.2f\n", total} ' item.$LOGNAME
    rm item.$LOGNAME
    echo ""
    echo "Purchased items"
    grep -v '|-' ushop.$LOGNAME > pur.$LOGNAME
    awk -F "|" ' BEGIN {total=0 }
    { printf "  %6d. %-30s %14.2f\n", NR, $1, $2
      total+=$2 }
    END { printf "                                          --------------\n" 
          printf "Total purchased items %33.2f\n", total} ' pur.$LOGNAME
    rm pur.$LOGNAME
    echo ""
    awk -F "|" ' BEGIN {total=0 } { total+=$2 }
    END { printf "GRAND TOTAL %43.2f\n", total} ' ushop.$LOGNAME
  fi
elif test "$1" = "-i"; then
  #clear
  price=0
  productname=""
  echo ""
  if [ $# -eq 1 ] 
  then 
    echo -n "Please input the product name. " 
    read productname
    echo ""
    echo -n "How much does the item cost? "
    read price
  elif [ $# -eq 2 ]
  then
    productname=$2
    echo -n "How much does the item cost? "
    read price
  elif [ $# -eq 3 ] 
  then
    productname=$2
    price=$3    
  fi
  if echo $price | grep '\<[0-9]*\>' > /dev/null; then
    echo ""
    echo "The item \"$productname\" priced at $price has been created."
    echo "$productname|$price|-" >> ushop.$LOGNAME
    echo "It has been added to your list of items to be purchased."
  else
    echo ""
    echo "Invalid number format."
  fi
elif [ "$1" = "-p" ]; then
  #clear
  touch temp.$LOGNAME
  if [ $# -eq 2 ]; then
    echo "$2 " > temp.$LOGNAME
    wordcount=$(wc -w temp.$LOGNAME | cut -d' ' -f1)
    tempcount=1
    rm temp.$LOGNAME
    touch temp.$LOGNAME
    grep '|-' ushop.$LOGNAME > item.$LOGNAME
    while [ $wordcount -ge $tempcount ]; do
      grep -i $(echo $2 | cut -d' ' -f$tempcount) item.$LOGNAME >> temp.$LOGNAME
      tempcount=`expr $tempcount + 1`
    done
    #                        #remove duplications in temp.$LOGNAME
    itemcount=$(grep -c '|' temp.$LOGNAME)
    awk -F "|" '{printf "%6d. %-30s %14.2f\n", NR, $1, $2 }' temp.$LOGNAME
  elif [ $# -eq 1 ]; then
    grep '|-' ushop.$LOGNAME > temp.$LOGNAME
    itemcount=$(grep -c '|' temp.$LOGNAME)
    awk -F "|" '{printf "%6d. %-30s %14.2f\n", NR, $1, $2 }' temp.$LOGNAME
  fi
  if [ $(wc -l temp.$LOGNAME | cut -d' ' -f1) -gt 0 ]; then
    echo ""
    echo -n "Indicate item number do you want to purchase (Enter purchases everything): " 
    read ans
      if [ "$ans" = "" ]; then
        echo ""; echo -n "Purchase all items above (y/n)? "
	read yorn
	if [ "$yorn" = "y" ]; then
       	  tempcount=1
          while [ $(wc -w temp.$LOGNAME | cut -d' ' -f1) -ge $tempcount ]; do
             item=$(cat temp.$LOGNAME | head -$tempcount | tail -1 | cut -d'|' -f1)
             price=$(cat temp.$LOGNAME | head -$tempcount | tail -1 | cut -d'|' -f2)
             cat ushop.$LOGNAME | sed s/"$item|$price|-"/"$item|$price|$date"/ > temp2.$LOGNAME
             cp temp2.$LOGNAME ushop.$LOGNAME
             tempcount=`expr $tempcount + 1`
          done
          echo ""; echo "All items listed above have been purchased."
	else
	  echo ""; echo "No item was purchased"
	fi
      elif test $ans -ge 1 && test $ans -le $itemcount; then
        product=$(cat temp.$LOGNAME | cut -d'|' -f1 | head -$ans | tail -1)
        echo ""; echo -n "Purchase the item \"$product\" (y/n)? "
        read yorn
        if [ "$yorn" = "y" ]; then
          grep -v "$product" ushop.$LOGNAME > pur.$LOGNAME
          grep -i "$product" ushop.$LOGNAME | sed s/-/"$date"/ >> pur.$LOGNAME
          cp pur.$LOGNAME ushop.$LOGNAME
          rm pur.$LOGNAME
          echo ""; echo "Item purchased."
        else
          echo ""; echo "Item was not purchased."
        fi
      else
        echo ""; echo "The number doesn't point to any item or invalid number format."
      fi 
  else
    echo "Did not find any matching item in the to-buy list."
  fi
  rm temp.$LOGNAME
fi
